/*
 * Directory.cpp
 *
 *  Created on: 18 apr. 2012
 *      Author: Harm
 */

#include "Directory.h"

Directory::Directory(Inode * inode, byte * b) :
	File(inode) {

	for (vector<int>::iterator it = inode->blocks.begin(); it
			!= inode->blocks.end(); it++) {
		int addr = (*it * 512);
		int num_files = inode->di_size / 16;
		for (int i = 0; i < num_files; i++) {
			int inode_nr = getshort(b, addr);
			string name = getchars(b, addr, 14);

			if (inode_nr != 0) {
				cout << addr << "  " << inode_nr << " |" << name << "| \n";
				files.insert(make_pair(name, inode_nr));
			}
		}
	}
}

void Directory::print() {
	for (map<string, int>::iterator it = files.begin(); it != files.end(); it++) {
		cout << it->second << " " << it->first << "\n";
	}
}

bool Directory::contains(string name) {

	return files.count(name);
}

int Directory::findFile(string name) {
	cout << "\n\n";
	for (map<string, int>::iterator it = files.begin(); it != files.end(); it++) {
		//cout << it->first << "\n";
	}
	return files.find(name)->second;
}

Directory::~Directory() {
	// TODO Auto-generated destructor stub
}
